package com.jydd.plug_in_study;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.jydd.plug_in_study.util.Util;

import java.io.File;
import java.io.IOException;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String ASSET_FILE_NAME = "plugin1.apk";
    private static final String ASSET_TARGET_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "plugin1.apk";
    private Button btn1, btn2;
    private ProgressBar mProgressbar;
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        Util.log("MainActivity, attachBaseContext");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Util.log("MainActivity, onCreate");
        initView();
        // 权限检测及授权
        RequestPermissionUtils requestPermissionUtils = new RequestPermissionUtils(this);
        requestPermissionUtils.requestPermissions(99);
        ProxyUtils.doHandlerHooker();
        ProxyUtils.doInstrumentationHook(this);
        File pluginFile = new File(ASSET_TARGET_FILE_PATH);
        if(!pluginFile.exists()){
            hideButton(btn1);
            hideButton(btn2);
            mProgressbar.setVisibility(View.VISIBLE);
            mProgressbar.setMax(100);
            Util.log("before - asset - copy - file - thread : " + Thread.currentThread().getName());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    copePluginFileToSdcard();
                }
            }).start();
        } else {
            // 文件已存在，显示Button
            showButton(btn1);
            showButton(btn2);
        }
    }
    private void copePluginFileToSdcard() {
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Util.log("asset - copy - file - thread : " + Thread.currentThread().getName());
        try {
            Util.copyAsset(this, ASSET_FILE_NAME, ASSET_TARGET_FILE_PATH, new Util.AssetFileCopyResult() {
                @Override
                public void done() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 文件完全复制完成，才显示Button控件，否则不显示
                            showButton(btn1);
                            showButton(btn2);
                            mProgressbar.setVisibility(View.GONE);
                            mProgressbar.setIndeterminate(true);
                        }
                    });

                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏Button
     * @param btn
     */
    private void hideButton(Button btn) {
        if(btn.getVisibility() == View.VISIBLE)
            btn.setVisibility(View.GONE);
    }

    /**
     * 显示Button
     * @param btn
     */
    private void showButton(Button btn){
        if(btn.getVisibility() != View.VISIBLE)
            btn.setVisibility(View.VISIBLE);
    }

    private void initView() {
        mProgressbar = findViewById(R.id.progressBar);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.renxh.pluginapp", "com.renxh.pluginapp.PluginActivity"));
                startActivity(intent);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                PluginHelper.loadPluginClass(MainActivity.this, MainActivity.class.getClassLoader());
                ResourceHelper.addResource(MainActivity.this, "/sdcard/plugin1.apk");
            }
        });
    }
}
